// ============================================================================
//
// Copyright (C) 2009-2011 SC Technology Group. - www.sc.com
//
// ============================================================================
package com.cs.vessel.breakdown.view.providers;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.common.util.EList;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;

import com.cs.vessel.breakdown.CDOHibernateUtil;
import com.sc.vessel.info.editor.AccidentInput;
import com.sc.vessel.info.model.breakdown.Accident;
import com.sc.vessel.info.model.breakdown.Component;

/**
 * DOC liaoqc class global comment. Detailled comment
 */
public class AccidentViewContentProvider implements IStructuredContentProvider {

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IContentProvider#dispose()
     */
    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object,
     * java.lang.Object)
     */
    @Override
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
     */
    @Override
    public Object[] getElements(Object inputElement) {
        LinkedList<Accident> accidentsList = new LinkedList<Accident>();
        if (inputElement instanceof AccidentInput) {
            Component component = ((AccidentInput) inputElement).getComponent();
            EList<Accident> accidents;
            try {
                accidents = getAllChildAccidents(component);
                accidentsList.addAll(accidents);
            } catch (CommitException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        return accidentsList.toArray();
    }

    /**
     * DOC liaoqc Comment method "getAllChildAccidents".
     * 
     * @param component
     * @return
     * @throws CommitException
     */
    private EList<Accident> getAllChildAccidents(Component component) throws CommitException {
        EList<Accident> accidents = CDOHibernateUtil.getBreakdowns(component);
        List<Component> componentList = new LinkedList<Component>();
        getAllChildComponents(component, componentList);
        if (componentList != null) {
            for (Iterator<Component> iterator = componentList.iterator(); iterator.hasNext();) {
                Component childComponent = (Component) iterator.next();
                accidents.addAll(CDOHibernateUtil.getBreakdowns(childComponent));
            }
        }
        return accidents;
    }

    /**
     * DOC liaoqc Comment method "getAllChildComponents".
     * 
     * @param component
     * @param componentList
     * @return
     */
    private void getAllChildComponents(Component component, List<Component> componentList) {
        EList<Component> components = component.getComponents();
        componentList.addAll(components);
        if (components == null || components.size() == 0) {
            return;
        } else {
            for (Iterator<Component> iterator = components.iterator(); iterator.hasNext();) {
                Component childComponent = (Component) iterator.next();
                getAllChildComponents(childComponent, componentList);
            }
        }
        return;
    }

}
